<template>
  <div class="login-container">
    <div class="iptbox">
      <img src="@/assets/img/icon_dl_sjh.png" class="ipticon1">
      <input placeholder="请输入旧手机号" class="ipt" v-model="phone">
    </div>
    <div class="iptbox">
      <img src="@/assets/img/icon_zc_yzm.png" class="ipticon2">
      <input placeholder="请输入验证码" class="ipt" v-model="yanzhengma">
      <div class="yanzhengbox" v-if="yanzheng == 0" @click="tohave">
        <span>获取验证码</span>
      </div>
      <div class="yanzhengbox" v-if="yanzheng == 1">
        <span>{{ count }}后重新获取验证码</span>
      </div>
    </div>
    <div class="iptbox">
      <img src="@/assets/img/icon_dl_sjh.png" class="ipticon1">
      <input placeholder="请输入新手机号" class="ipt" v-model="newphone">
    </div>
    <div class="loginbtn" @click="register">修改</div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      phone: "",
      newphone: "",
      yanzhengma: "",
      yanzheng: 0,
      reallyyanzhengma: "",
      count: 60,
      timer: null
    };
  },
  methods: {
    register() {
      let that = this;
      let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (that.phone == "" || that.yanzhengma == "" || that.password == "") {
        that.$toast({
          message: "请完善信息"
        });
      }
      if (!myreg.test(that.phone)) {
        that.$toast({
          message: "请输入正确手机号"
        });
      }
      if (!myreg.test(that.newphone)) {
        that.$toast({
          message: "请输入正确手机号"
        });
      }
      var Sukdata = {
        phone: that.phone,
        newPhone: that.newphone,
        code: that.yanzhengma
      };
      // console.log(obj)
      that.axios
        .post(that.baseUrl + "/user/changePhone", that.Qs.stringify(Sukdata), {
          headers: {
            "Content-Type": "application/x-www-form-urlencoded"
          }
        })
        .then(function(res) {
          if (res.request.status === 200) {
            console.log(res.data);
            if (res.data.meta.code == 200) {
              localStorage.setItem("phone", res.data.data.phone);
              localStorage.setItem("userid", res.data.data.id);
            } else if (res.data.meta.code == 501) {
              that.$toast({
                message: res.data.meta.msg
              });
            }
          }
        });
    },
    tohave() {
      let that = this;
      let myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
      if (!myreg.test(that.phone)) {
        that.$toast({
          message: "请输入正确手机号"
        });
        return false;
      } else {
        let Sukdata = {
          phone: that.phone
        };
        that.axios
          .post(that.baseUrl + "/user/sendsmsLp", that.Qs.stringify(Sukdata), {
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            }
          })
          .then(function(res) {
            if (res.request.status === 200) {
              console.log(res.data);
              if (res.data.meta.code == 200) {
              } else if (res.data.meta.code == 501) {
              }
            }
          });

        const timer = setInterval(() => {
          that.count--;
          console.log(that.count);
          if (that.count == 0) {
            clearInterval(timer);
            that.yanzheng = 0;
            that.count = 60;
          }
        }, 1000);
        that.count = 60;
        that.yanzheng = 1;
      }
    },
    toregister() {
      this.$router.push({
        name: "register"
      });
    },
    tologin() {
      this.$router.push({
        name: "login"
      });
    }
  }
};
</script>
<style scoped>
.login-other {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding-bottom: 30px;
}
.yanzhengbox {
  padding-left: 1%;
  padding-right: 1%;
  position: absolute;
  right: 5%;
  display: flex;
  align-items: center;
  text-align: center;
  justify-content: center;
  height: 50%;
  color: #ff2f4e;
  border-radius: 10px;
  border: 1px solid #ff2f4e;
}
.loginbtn {
  width: 80%;
  margin-top: 15px;
  text-align: center;
  background: #ff2f4e;
  margin: 25px auto;
  height: 30px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  border-radius: 8px;
}
.other-model {
  margin-top: 30px;
  text-align: center;
}
.other-qq {
  display: flex;

  justify-content: center;
}
.other-img {
  width: 40%;
  margin-bottom: 10px;
}
.other-tip {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
  align-items: center;
  color: #8a8a8a;
}
.login-border {
  display: inline-block;
  height: 0;
  width: 34%;
  border-bottom: #e3e3e3 solid 1px;
}
.login-container {
  font-size: 0.3rem;
  height: 100vh;
  width: 100%;
}
.title {
  padding-top: 20px;
}
.ipticon1 {
  width: 13px;
  height: 20px;
  vertical-align: middle;
  margin-left: 17px;
}
.iptbox {
  position: relative;
  width: 90%;
  /* height: 6%; */
  height: 35px;
  display: flex;
  align-items: center;
  border: 1px solid #dcdcdc;
  border-radius: 20px;
  margin: 20px auto;
  text-align: left;
}
.ipticon2 {
  width: 16px;
  margin-left: 17px;
  height: 20px;
  vertical-align: middle;
}
.ipt {
  border: none;
  color: #8a8a8a;
  height: 50%;
  margin-left: 12px;
}
h2 {
  text-align: center;
}
.bbox {
  width: 90%;
  display: flex;
  justify-content: space-between;
  margin: 0 auto;
  color: #ff2f4e;
}
</style>
